package app.framework.security;

import app.framework.config.FrontGlobalConfig;
import app.framework.security.captcha.CaptchaFilter;
import jasmine.autoconfigure.security.JasmineSecurityProperties;
import jasmine.autoconfigure.security.SpringSecurityConfiguration;
import jasmine.framework.common.util.SpringUtil;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @author mh.z
 */
public class CustomSpringSecurityConfiguration extends SpringSecurityConfiguration {
    private FrontGlobalConfig frontConfig;

    public CustomSpringSecurityConfiguration(JasmineSecurityProperties securityProperties,
                                             FrontGlobalConfig frontConfig) {
        super(securityProperties);
        this.frontConfig = frontConfig;
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        super.configure(http);

        if (Boolean.TRUE.equals(frontConfig.getCaptchaEnabled())) {
            CaptchaFilter captchaFilter = SpringUtil.getBean(applicationContext,
                    CaptchaFilter.class, true);

            http.addFilterBefore(captchaFilter, UsernamePasswordAuthenticationFilter.class);
        }
    }

}
